package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO 菜品数据传输对象
     * @return 成功结果
     */
    @PostMapping
    @ApiOperation(value = "新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品: {}", dishDTO);
        dishService.saveWithFlavor(dishDTO);// 调用服务层方法保存菜品和口味信息
        return Result.success();
    }
    /**
     * page分页查询
     * @param dishPageQueryDTO
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询：{}",dishPageQueryDTO);
        PageResult pageResult  = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 删除菜品
     * @param ids
     */
    @DeleteMapping
    @ApiOperation(  value = "菜品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品，id列表：{}", ids);
        dishService.deleteBatch(ids);
        return Result.success();
    }

    /**
     *根据id查询菜品
     *
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询菜品")
    public Result<DishVO> getByid(@PathVariable Long id){
        log.info("根据id查询菜品，id：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);

    }
    /**
     * 更新菜品
     * @param dishDTO 菜品数据传输对象
     * @return 成功结果
     */
    @PutMapping
    @ApiOperation(value = "更新菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("更新菜品: {}", dishDTO);
        dishService.updateWithFlavor(dishDTO); // 调用服务层方法更新菜品和口味信息
        return Result.success();

    }

}
